// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/container/f14_set.h>
#include <melon/init/init.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>
#include <pollux/vector/vector_fuzzer.h>

using namespace kumo::pollux;
using namespace kumo::pollux::exec;
using namespace kumo::pollux::test;

namespace {

/// Fast implementation of IN (a, b, c,..) using melon::F14FastSet.
VectorPtr fastIn(
    const melon::F14FastSet<int32_t>& inSet,
    const VectorPtr& data) {
  const auto numRows = data->size();
  auto result = std::static_pointer_cast<FlatVector<bool>>(
      BaseVector::create(BOOLEAN(), numRows, data->pool()));
  auto rawResults = result->mutableRawValues<int32_t>();

  auto rawData = data->as_unchecked<FlatVector<int32_t>>()->rawValues();
  for (auto row = 0; row < numRows; ++row) {
    bits::setBit(rawResults, row, inSet.contains(rawData[row]));
  }

  return result;
}

class InBenchmark : public functions::test::FunctionBenchmarkBase {
 public:
  InBenchmark() : FunctionBenchmarkBase() {
    functions::prestosql::registerGeneralFunctions();
  }

  RowVectorPtr makeData() {
    VectorFuzzer::Options opts;
    opts.vectorSize = 1'000;
    return vectorMaker_.row_vector(
        {VectorFuzzer(opts, pool()).fuzzFlat(INTEGER())});
  }

  void run(size_t numValues) {
    melon::BenchmarkSuspender suspender;
    auto data = makeData();

    std::ostringstream inList;
    inList << "0";
    for (auto i = 1; i < numValues; ++i) {
      inList << ", " << i * 2;
    }

    auto sql = fmt::format("c0 IN ({})", inList.str());
    auto exprSet = compileExpression(sql, data->type());
    suspender.dismiss();

    doRun(exprSet, data);
  }

  void doRun(ExprSet& exprSet, const RowVectorPtr& row_vector) {
    int cnt = 0;
    for (auto i = 0; i < 1000; i++) {
      cnt += evaluate(exprSet, row_vector)->size();
    }
    melon::doNotOptimizeAway(cnt);
  }

  void runFast(size_t numValues) {
    melon::BenchmarkSuspender suspender;
    auto data = makeData();

    melon::F14FastSet<int32_t> inSet;
    inSet.reserve(numValues);
    for (auto i = 0; i < numValues; ++i) {
      inSet.insert(i);
    }
    suspender.dismiss();

    doRunFast(inSet, data->childAt(0));
  }

  void doRunFast(
      const melon::F14FastSet<int32_t>& inSet,
      const VectorPtr& data) {
    int cnt = 0;
    for (auto i = 0; i < 1000; i++) {
      cnt += fastIn(inSet, data)->size();
    }
    melon::doNotOptimizeAway(cnt);
  }
};

BENCHMARK(fastIn) {
  InBenchmark benchmark;
  benchmark.runFast(10);
}

BENCHMARK_RELATIVE(in) {
  InBenchmark benchmark;
  benchmark.run(10);
}

BENCHMARK(fastIn1K) {
  InBenchmark benchmark;
  benchmark.runFast(1'000);
}

BENCHMARK_RELATIVE(in1K) {
  InBenchmark benchmark;
  benchmark.run(1'000);
}

} // namespace

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};

  melon::runBenchmarks();
  return 0;
}
